package com.starwish.starwishblog.web.back;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.PermissionService;
import com.starwish.starwishblog.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/permission")
@Api(value = "操作权限的接口")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询权限")
    public ResultResponse<PageInfo<PermissionVo>> list(@ApiParam(name="now",value = "当前页")@RequestParam(name="now",defaultValue = "1")Integer now,
                                                       @ApiParam(name="size",value = "页大小")@RequestParam(name="size",defaultValue = "5")Integer size,
                                                       @ApiParam(name="name",value = "搜索条件")String name){
        ResultResponse<PageInfo<PermissionVo>> result = null;
        try {
            PageInfo<PermissionVo> pageInfo = permissionService.queryPermissionsByCons(now, size, name);
            result = new ResultResponse<>(200,"查询权限成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"系统错误");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> delById(@PathVariable(name="id")Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = permissionService.removePermissionById(id);
            if(i > 0){
                result = new ResultResponse<>(200,"删除成功");
            }else{
                result = new ResultResponse<>(201,"删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @GetMapping("/getParents")
    public ResultResponse<List<PermissionVo>> getParents(){
        ResultResponse<List<PermissionVo>> result = null;
        try {
            List<PermissionVo> parentPermissions = permissionService.queryParentPermissions();
            if(parentPermissions != null){
                result = new ResultResponse<>(200,"查询父菜单成功",parentPermissions);
            }else{
                result = new ResultResponse<>(201,"查询父菜单失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @PutMapping("/add")
    public ResultResponse<Void> add(@RequestBody PermissionVo permissionVo){
        ResultResponse<Void> result = null;
        try {
            int i = permissionService.addPermission(permissionVo);
            if(i > 0){
                result = new ResultResponse<>(200,"添加成功");
            }else{
                result = new ResultResponse<>(201,"添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody PermissionVo permissionVo){
        ResultResponse<Void> result = null;
        try {
            int i = permissionService.modifyPermission(permissionVo);
            if(i > 0){
                result = new ResultResponse<>(200,"修改成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

}
